package com.blb.examsystem.controller;

import com.blb.common.entity.PageBean;
import com.blb.common.entity.QuestionSet;

import com.blb.examsystem.service.QuestionSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletException;
import java.io.IOException;

@Controller
public class QuestionSetServlet{
    @Autowired
    private QuestionSetService questionSetService ;
    @RequestMapping("/findSetPage")
    public String findPage(@RequestParam(value = "current",required = false,defaultValue = "1") Integer current, Model model)  {
        PageBean<QuestionSet> page = questionSetService.findPage(current);
        //将数据传到更新页面
        model.addAttribute("page",page);
        return "questionSets";
    }

    //按标题查找
    @RequestMapping("/searchByTitle")
    public String searchByTitle(String title,Model model,@RequestParam(value = "current",required = false,defaultValue = "1") Integer current) throws ServletException, IOException {
        //获取参数
        PageBean<QuestionSet> page = questionSetService.findPageByTitle(title, current);
        model.addAttribute("page",page);
        return "questionSets";
    }

    //添加
    @RequestMapping("/addQuestionSet1")
    public String addQuestionSet(QuestionSet questionSet) throws ServletException, IOException {
        int i = questionSetService.addQuestionSet(questionSet);
        return "redirect:/findSetPage";
    }

    //按id查找
    @RequestMapping("/findQuestionSetById")
    public String findQuestionSetById(Integer id ,Model model) throws ServletException, IOException {
        QuestionSet questionSet = questionSetService.findById(id);
        model.addAttribute("questionSet",questionSet);
        return "updateQuestionSet";
    }

    //更新
    @RequestMapping("/modifyQuestionSet")
    public String modifyQuestionSet(QuestionSet questionSet ) throws ServletException, IOException {
        System.out.println(questionSet);
        int i = questionSetService.modifyQuestionSet(questionSet);
        return "redirect:/findSetPage";
    }

    //删除
    @RequestMapping("/removeQuestionSet")
    public String removeQuestionSet(Integer id) throws ServletException, IOException {
        questionSetService.removeQuestionSet(id);
        return "redirect:/findSetPage";
    }

}
